<template>
  <div>
    <!-- 注册的弹出表单 -->
    <Modal v-model="show" @on-cancel="cancel" :mask-closable="false">
      <Form ref="formInline" :model="formInline" :rules="ruleInline" inline>
        <FormItem prop="email" style="display:inline">
          <Input type="text" v-model="formInline.email" placeholder="请输入邮箱">
            <Icon type="ios-mail" slot="prepend"></Icon>
          </Input>
        </FormItem>
        <FormItem prop="code" style="display:inline">
          <Input type="text" v-model="formInline.code" placeholder="请输入验证码">
            <Button type="primary" size="large" @click="send">发送验证码</Button>
          </Input>
        </FormItem>
        <FormItem prop="password" style="display:inline">
          <Input type="password" v-model="formInline.password" placeholder="请输入密码">
            <Icon type="ios-lock-outline" slot="prepend"></Icon>
          </Input>
        </FormItem>
        <FormItem prop="repassword" style="display:inline">
          <Input type="password" v-model="formInline.repassword" placeholder="请重新输入密码">
            <Icon type="ios-lock-outline" slot="prepend"></Icon>
          </Input>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" size="large" @click="cancel">取消</Button>
        <Button type="primary" size="large" :loading="loading" @click="ok">重置</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import crypto from "crypto";
export default {
  name: "LoginModal",
  props: ["show_parent"],
  data() {
    return {
      show: this.show_parent,
      loading: false,
      error_msg: "",
      formInline: {
        code: "",
        email: "",
        password: "",
        repassword: ""
      },
      ruleInline: {
        //表单验证规则
        password: [
          {
            validator: (rule, value, callback) => {
              var reg = new RegExp(
                "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z\\W]{6,18}$"
              );
              if (!reg.test(value)) {
                callback(true);
              } else {
                callback();
              }
            },
            message: "密码必须为6-18位，只能包括数字、字母、特殊字符",
            trigger: "change,blur"
          }
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "change,blur" },
          { type: "email", message: "邮箱格式不正确", trigger: "change,blur" }
        ],
        repassword: [
          {
            validator: (rule, value, callback) => {
              if (value != this.formInline.password) {
                callback(true);
              } else {
                callback();
              }
            },
            message: "两次输入的密码不一样",
            trigger: "change,blur"
          }
        ],
        code: [
          {
            validator: (rule, value, callback) => {
              if (len(rule) != 6) {
                callback(true);
              } else {
                callback();
              }
            },
            message: "验证码必须是6位",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    ok() {
      //点击注册按钮
      this.$refs["formInline"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$api.post(
            "forget/",
            {
             
            },
            response => {
              this.loading = false;
              var params = { registered: true };
              if (response.status != 200) {
                this.$Message.error("注册失败，服务器错误");
                console.log(response);
                this.$Message.error("" + response);
              } else {
                var data = response.data;
                console.log(data);
                if (data.err_code==0) {
                  this.show = false;
                  this.$Message.success("注册成功，请您登录");
                  this.$emit("registerClosed", params);
                } else {
                  this.$Message.error(data.error);
                }
              }
            }
          );
        } else {
          this.$Message.error("请更正信息后注册");
          this.$refs.modal.buttonLoading = false;
        }
      });
    },
    cancel() {
      //点击取消或右上角叉号
      var params = { registered: false };
      this.show = false;
      this.$emit("registerClosed", params);
    },
    send() {
      //点击发送验证码按钮
      this.$api.get("/forget", { email: this.email }, response => {
        if (response.status != 200) {
          this.$Message.error("发送失败，服务器错误");
          console.log(response);
          this.$Message.error("" + response);
        } else {
            data=resompose.data
          if (data.err_code == 0) {
            this.$Message.success("发送成功");
          } else {
              this.$Message.error("发送失败");
              this.$Message.error(data.error);
          }
        }
      });
    },
    setMd5(text) {
      //加密密码
      var md5 = crypto.createHash("md5");
      md5.update(text);
      var text_md5 = md5.digest("hex");
      return text_md5;
    }
  }
};
</script>
<style scoped>
</style>
